var currentQuestionIndex = 0;
var correctAnswers = 0;

var questions = [
    { question: "中国古代的'四大发明'是指什么？", options: ["火药、指南针、印刷术、纸张", "灯笼、瓷器、扇子、丝绸", "书法、绘画、音乐、舞蹈", "茶叶、瓷器、丝绸、漆器"], correctAnswer: 0, explanation: "四大发明是指火药、指南针、印刷术、纸张。" },
    { question: "以下哪位诗人被称为'诗仙'？", options: ["杜牧", "李白", "王之涣", "杜甫"], correctAnswer: 1, explanation: "李白被称为'诗仙'。" },
    { question: "《红楼梦》是中国古典小说的巅峰之作，它的作者是谁？", options: ["曹雪芹", "施耐庵", "吴承恩", "罗贯中"], correctAnswer: 0, explanation: "《红楼梦》的作者是曹雪芹。" },
    { question: "中国的'五岳'是指哪五座山？", options: ["泰山、华山、恒山、黄山、嵩山", "青藏高原的五座山", "峨眉山、庐山、衡山、武当山、恒山", "华山、黄山、嵩山、太行山、秦岭"], correctAnswer: 0, explanation: "五岳是指泰山、华山、恒山、黄山、嵩山。" },
    { question: "中国的'春节'是根据哪种农历来确定的？", options: ["阴历", "阳历", "月历", "月历"], correctAnswer: 0, explanation: "春节是根据阴历来确定的。" },
    { question: "中国的'长城'是哪个朝代修建的？", options: ["春秋战国时期", "秦朝", "汉朝", "唐朝"], correctAnswer: 1, explanation: "长城是在秦朝修建的。" },
    { question: "中国古代的'礼仪之邦'是指哪个时期？", options: ["春秋战国时期", "唐朝", "元朝", "清朝"], correctAnswer: 0, explanation: "礼仪之邦是指春秋战国时期。" },
    { question: "中国传统的'九九重阳节'是在每年的哪一天？", options: ["农历九月初九", "农历八月十八", "农历九月初一", "农历十月初十"], correctAnswer: 0, explanation: "九九重阳节是农历九月初九。" },
    { question: "中国的国花是什么？", options: ["牡丹", "菊花", "玫瑰", "芙蓉"], correctAnswer: 0, explanation: "中国的国花是牡丹。" },
    { question: "中国的'黄帝内经'是哪个学科的经典？？", options: ["军事学", "医学", "哲学", "农学"], correctAnswer: 1, explanation: "黄帝内经是医学经典。" }
    // Add more questions as needed
];

function displayQuestion() {
    var currentQuestion = questions[currentQuestionIndex];
    document.getElementById("question").innerHTML = currentQuestion.question;

    var buttons = document.getElementsByClassName("button");
    for (var i = 0; i < buttons.length; i++) {
        buttons[i].innerHTML = currentQuestion.options[i];
        buttons[i].setAttribute("data-index", i); // Add data-index attribute for identifying button index
    }
}

function checkAnswer(selectedOption) {
    var currentQuestion = questions[currentQuestionIndex];
    var correctIndex = currentQuestion.correctAnswer;

    if (selectedOption === correctIndex) {
        // Correct answer
        correctAnswers++;
    } else {
        // Incorrect answer
        document.getElementById("modal-title").innerHTML = "Incorrect!";
        document.getElementById("modal-text").innerHTML = currentQuestion.explanation;
        document.getElementById("modal-container").style.display = "block";
        return; // Stop here for incorrect answers
    }

    // Move to the next question
    displayNextQuestion();
}

function displayNextQuestion() {
    currentQuestionIndex++;
    if (currentQuestionIndex < questions.length) {
        // Display the next question
        displayQuestion();
    } else {
        // No more questions, display results
        displayResults();
    }
}

function displayResults() {
    var accuracy = (correctAnswers / questions.length) * 100;
    var message = "这是我逼着你选的正确答案啊\n";
    message += "我的而不是你的准确率是 " + accuracy.toFixed(2) + "%";
    alert(message);
}

function closeModal() {
    document.getElementById("modal-container").style.display = "none";
}

function nextQuestion() {
    if (currentQuestionIndex < questions.length) {
        alert("问题都没有回答完，瞎点什么啊");
    } else {
        // Display results
        displayResults();
    }
}

// Event delegation for button clicks
document.addEventListener("click", function (event) {
    if (event.target.classList.contains("button")) {
        var selectedOption = parseInt(event.target.getAttribute("data-index"));
        checkAnswer(selectedOption);
    }
});

// Event delegation for "确定" button
document.getElementById("sex").addEventListener("click", function () {
    nextQuestion();
});

// Initial display
displayQuestion();
